package com.hundredhealth.rose.activity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hundredhealth.rose.HealthLabelApp;
import com.hundredhealth.rose.R;
import com.hundredhealth.rose.base.BaseFrameActivity;
import com.hundredhealth.rose.delegate.CustomerDetailDelegate;
import com.hundredhealth.rose.task.TaskOpen;
import com.lvrenyang.io.IOCallBack;

public class CustomerDetailActivity extends BaseFrameActivity<CustomerDetailDelegate> implements View.OnClickListener, IOCallBack {

    private static final String TAG = "CustomerPrintActivity";
    public static final String KEY_CUSTOMER = "CUSTOMER";
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
    private static final int REQUEST_CODE_PERMISSION = 3;

    private BluetoothAdapter mBluetoothAdapter = null;

    @Override
    protected Class<CustomerDetailDelegate> getDelegateClass() {
        return CustomerDetailDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.printButton).setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "蓝牙当前不可用！", Toast.LENGTH_LONG).show();
            viewDelegate.disablePrintButton();
        }
        HealthLabelApp.mBTPrinting.SetCallBack(this);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onStart() {
        super.onStart();
        // If Bluetooth is not on, request that it be enabled.
        // setupChat() will then be called during onActivityResult
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(
                    BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
            // Otherwise, setup the session
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        HealthLabelApp.mBTPrinting.SetCallBack(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CONNECT_DEVICE && resultCode == Activity.RESULT_OK) {
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras().getString(SearchBLEActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                if (BluetoothAdapter.checkBluetoothAddress(address)) {
                    HealthLabelApp.executorService.submit(new TaskOpen(address, this));
                }
            }
        }
        if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a session
                //KeyListenerInit();TODO
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    public void OnOpen() {
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                HealthLabelApp.bluetooth_ready = true;
                viewDelegate.printLabel();
                Toast.makeText(CustomerDetailActivity.this, "Connected", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void OnOpenFailed() {
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                HealthLabelApp.bluetooth_ready = false;
                Toast.makeText(CustomerDetailActivity.this, "Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void OnClose() {
        this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                HealthLabelApp.bluetooth_ready = false;
                Toast.makeText(CustomerDetailActivity.this, "Closed", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.printButton: {
                if (viewDelegate.doValidate()) {
                    viewDelegate.updatePrintCount();
                    if (HealthLabelApp.bluetooth_ready) {
                        viewDelegate.printLabel();
                    } else {
                        Intent serverIntent = new Intent(CustomerDetailActivity.this, SearchBLEActivity.class);
                        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                    }
                }
                break;
            }
        }
    }
}
